<template>
  <el-dialog
    :title="FormList.id?'编辑经纪':'添加经纪'"
    :visible="showDialog"
    width="50%"
    @close="closeFn"
  >
    <el-form ref="form" :rules="rules" :model="FormList" label-width="100px">
      <el-form-item label="名称" prop="name">
        <el-input v-model="FormList.name" />
      </el-form-item>
      <el-form-item label="邮箱" prop="email">
        <el-input v-model="FormList.email" />
      </el-form-item>
      <el-form-item label="电话" prop="phone">
        <el-input v-model="FormList.phone" />
      </el-form-item>
      <el-form-item label="地址" prop="address">
        <el-input v-model="FormList.address" />
      </el-form-item>
      <el-form-item label="描述" prop="desc">
        <el-input v-model="FormList.desc" />
      </el-form-item>
      <el-form-item label="封面" prop="image">
        <!-- 放置上传图片 -->
        <UploadImage ref="img1" />
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button type="primary" size="small" @click="submit">确定</el-button>
      <el-button size="small" @click="etiFn">取消</el-button>
    </span>
  </el-dialog>
</template>

<script>
import { addJingJiList, edtiJingJiList, updateJingJiList } from '@/api/jingji'

export default {
  props: {
    showDialog: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      FormList: {
        name: '',
        email: '',
        phone: '',
        address: '',
        desc: '',
        image: ''
      }, // 表单数据
      rules: {
        name: [
          { required: true, message: '该项不能为空', trigger: 'blur' }
        ],
        desc: [
          { required: true, message: '该项不能为空', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    // 获取单个经纪详情
    async edtiJingJiList(id) {
      const res = await edtiJingJiList(id)
      console.log(84, res)
      this.FormList = res.data
    },
    // 取消新增
    etiFn() {
      this.closeFn()
    },
    // 确定新增  ||  编辑
    async submit() {
      // 表单校验
      await this.$refs.form.validate()
      // 判断是编辑还是新增
      if (this.FormList.id) {
        // 编辑
        await updateJingJiList(this.FormList)
      } else {
        // 新增
        // 回显图片
        this.FormList.image = this.$refs.img1.imgUrl
        //   调用新增接口
        await addJingJiList(this.FormList)
      }

      //   关闭弹窗
      this.closeFn()
      // 更新页面的经纪管理
      this.$emit('updateEvent')
      // 更新页面页码数量
      this.$emit('updateTotal')
    },
    closeFn() {
      this.$emit('update:showDialog', false)
      // 清空校验规则
      this.$refs.form.resetFields()
      //   清空数据
      this.FormList = {
        name: '',
        email: '',
        phone: '',
        address: '',
        desc: '',
        image: ''
      }
    }
  }
}
</script>

<style lang="scss" scoped>
::v-deep .el-dialog__header {
    background-color: #fff;
}
::v-deep .el-dialog__header .el-dialog__title {
  color: #303133;
}
::v-deep .el-input__inner {
    width: 652px;
    margin: 0 -34px;
}
::v-deep .el-form-item__label {
    padding: 0px 49px 0 0;
}
::v-deep .el-dialog__header .el-icon-close:before {
    color: #303133;
}
</style>
